package com.jgsu.farm.mapper.sql;
import com.jgsu.farm.entity.PestStress;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public interface PestStressMapperSql {
    @Insert("insert into peststress(stressType,cropID,chineseName,latinName,alias,category,distribution,morphology,lifeHabit,damage,control,pestImage,syptomImage) values(#{stresstype},#{cropid},#{chinesename},#{latinname},#{alias},#{category},#{distribution},#{morphology},#{lifehabit},#{damage},#{control},#{pestimage},#{syptomimage})")
    @Options(useGeneratedKeys = true, keyProperty = "pestid", keyColumn = "pestID")
    Integer add(PestStress peststress);

    @Select("select * from peststress where chineseName = #{chinesename}")
    Integer selectByName(PestStress peststress);

    @Select("select * from peststress where pestID = #{pestid}")
    Integer selectById(Integer pestid);

    @Delete("delete from peststress where pestID = #{pestid}")
    void delete(Integer pestid);

    @Results(id = "pestStressMap", value = {
            @Result(id = true, column = "pestid", property = "pestid"),
            @Result(column = "stresstype", property = "stresstype"),
            @Result(column = "stresstype", property = "stressCategory", one = @One(select = "com.jgsu.farm.mapper.sql.StressCategoryMapperSql.selectStressCategoryById", fetchType = FetchType.EAGER)),
            @Result(column = "cropid", property = "cropid"),
            @Result(column = "cropid", property = "plant", one = @One(select = "com.jgsu.farm.mapper.sql.PlantMapperSql.selectPlantById", fetchType = FetchType.EAGER)),
            @Result(column = "chinesename", property = "chinesename"),
            @Result(column = "latinname", property = "latinname"),
            @Result(column = "alias", property = "alias"),
            @Result(column = "category", property = "category"),
            @Result(column = "category", property = "biologyCategory",one = @One(select = "com.jgsu.farm.mapper.sql.BiologyMapperSql.selectCategoryById", fetchType = FetchType.EAGER)),
            @Result(column = "distribution", property = "distribution"),
            @Result(column = "morphology", property = "morphology"),
            @Result(column = "lifehabit", property = "lifehabit"),
            @Result(column = "damage", property = "damage"),
            @Result(column = "control", property = "control"),
            @Result(column = "pestimage", property = "pestimage"),
            @Result(column = "pestimage", property = "matchPestImage", many = @Many(select = "com.jgsu.farm.mapper.sql.MatchImageMapperSql.findMatchImageByPlantId", fetchType = FetchType.LAZY)),
            @Result(column = "syptomimage", property = "syptomimage"),
            @Result(column = "syptomimage", property = "matchSymptomImage", many = @Many(select = "com.jgsu.farm.mapper.sql.MatchImageMapperSql.findMatchImageByPlantId", fetchType = FetchType.LAZY))
    })
    @Select("select * from peststress")
    List<PestStress> selectAll();

    @Update("update peststress set stressType = #{stresstype},cropID = #{cropid},chineseName = #{chinesename},latinName = #{latinname},alias = #{alias},category = #{category},distribution = #{distribution},morphology = #{morphology},lifeHabit = #{lifehabit},damage = #{damage},control = #{control},pestImage = #{pestimage},syptomImage = #{syptomimage} where pestID = #{pestid}")
    Integer modify(PestStress peststress);
}
